home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 8 / Night Owl CD-ROM (NOPV8) (Night Owl Publisher) (1993).ISO / 047a / lex_yacc.arj / PAS.Y < prev    next >
Text File  |  1989-03-02  |  13KB  |  574 lines

  1. %{
  2. (*
  3.  * grammar.y
  4.  *
  5.  * Pascal grammar in Yacc format, based originally on BNF given
  6.  * in "Standard Pascal -- User Reference Manual", by Doug Cooper.
  7.  * This in turn is the BNF given by the ANSI and ISO Pascal standards,
  8.  * and so, is PUBLIC DOMAIN. The grammar is for ISO Level 0 Pascal.
  9.  * The grammar has been massaged somewhat to make it LALR, and added
  10.  * the following extensions.
  11.  *
  12.  * constant expressions
  13.  * otherwise statement in a case
  14.  * productions to correctly match else's with if's
  15.  * beginnings of a separate compilation facility
  16.  *
  17.  * - Pas.y: adapted for Turbo Pascal Yacc 2-28-89 AG
  18.  *)
  19.  
  20. uses PasLex, LexLib, YaccLib;
  21. procedure yymsg(msg : string);
  22.   begin
  23.     writeln(infile, ':', line_no, ': ', msg, ' at or before `', act_token, '''.')
  24.   end(*yymsg*);
  25.  
  26. %}
  27.  
  28. /* note the Pascal keyword tokens are stropped with leading underscore
  29.    (e.g. _AND) in the Turbo Pascal Yacc version, because these identifiers
  30.    will be declared as token numbers in the header file Pas.h generated
  31.    by Yacc, and hence must not collide with Turbo Pascal keywords.
  32.    3-1-89 AG */
  33.  
  34. %token _AND _ARRAY ASSIGNMENT _BEGIN _CASE CHARACTER_STRING COLON COMMA _CONST DIGSEQ
  35. %token _DIV _DO DOT DOTDOT _DOWNTO _ELSE _END EQUAL _EXTERNAL _FILE _FOR _FORWARD _FUNCTION
  36. %token GE _GOTO GT IDENTIFIER _IF _IN _LABEL LBRAC LE LPAREN LT MINUS _MOD _NIL _NOT
  37. %token NOTEQUAL _OF _OR _OTHERWISE _PACKED PLUS _PROCEDURE _PROGRAM RBRAC
  38. %token REALNUMBER _RECORD _REPEAT RPAREN SEMICOLON _SET SLASH STAR STARSTAR _THEN
  39. %token _TO _TYPE _UNTIL UPARROW _VAR _WHILE _WITH
  40.  
  41. %token ILLEGAL
  42.  
  43. %%
  44. file : program
  45.     | program error
  46.     { writeln(infile, ':', line_no,
  47.             ':Text follows logical end of program.'); }
  48.     | module
  49.     ;
  50.  
  51. program : program_heading semicolon block DOT
  52.     ;
  53.  
  54. program_heading : _PROGRAM identifier
  55.     | _PROGRAM identifier LPAREN identifier_list RPAREN
  56.     ;
  57.  
  58. identifier_list : identifier_list comma identifier
  59.     | identifier
  60.     ;
  61.  
  62. block : label_declaration_part
  63.     constant_definition_part
  64.     type_definition_part
  65.     variable_declaration_part
  66.     procedure_and_function_declaration_part
  67.     statement_part
  68.     ;
  69.  
  70. module : constant_definition_part
  71.     type_definition_part
  72.     variable_declaration_part
  73.     procedure_and_function_declaration_part
  74.     ;
  75.  
  76. label_declaration_part : _LABEL label_list semicolon
  77.     |
  78.     ;
  79.  
  80. label_list : label_list comma label
  81.     | label
  82.     ;
  83.  
  84. label : DIGSEQ
  85.     ;
  86.  
  87. constant_definition_part : _CONST constant_list
  88.     |
  89.     ;
  90.  
  91. constant_list : constant_list constant_definition
  92.     | constant_definition
  93.     ;
  94.  
  95. constant_definition : identifier EQUAL cexpression semicolon
  96.     ;
  97.  
  98. /*constant : cexpression ;        /* good stuff! */
  99.  
  100. cexpression : csimple_expression
  101.     | csimple_expression relop csimple_expression
  102.     ;
  103.  
  104. csimple_expression : cterm
  105.     | csimple_expression addop cterm
  106.     ;
  107.  
  108. cterm : cfactor
  109.     | cterm mulop cfactor
  110.     ;
  111.  
  112. cfactor : sign cfactor
  113.     | cexponentiation
  114.     ;
  115.  
  116. cexponentiation : cprimary
  117.     | cprimary STARSTAR cexponentiation
  118.     ;
  119.  
  120. cprimary : identifier
  121.     | LPAREN cexpression RPAREN
  122.     | unsigned_constant
  123.     | _NOT cprimary
  124.     ;
  125.  
  126. constant : non_string
  127.     | sign non_string
  128.     | CHARACTER_STRING
  129.     ;
  130.  
  131. sign : PLUS
  132.     | MINUS
  133.     ;
  134.  
  135. non_string : DIGSEQ
  136.     | identifier
  137.     | REALNUMBER
  138.     ;
  139.  
  140. type_definition_part : _TYPE type_definition_list
  141.     |
  142.     ;
  143.  
  144. type_definition_list : type_definition_list type_definition
  145.     | type_definition
  146.     ;
  147.  
  148. type_definition : identifier EQUAL type_denoter semicolon
  149.     ;
  150.  
  151. type_denoter : identifier
  152.     | new_type
  153.     ;
  154.  
  155. new_type : new_ordinal_type
  156.     | new_structured_type
  157.     | new_pointer_type
  158.     ;
  159.  
  160. new_ordinal_type : enumerated_type
  161.     | subrange_type
  162.     ;
  163.  
  164. enumerated_type : LPAREN identifier_list RPAREN
  165.     ;
  166.  
  167. subrange_type : constant DOTDOT constant
  168.     ;
  169.  
  170. new_structured_type : structured_type
  171.     | _PACKED structured_type
  172.     ;
  173.  
  174. structured_type : array_type
  175.     | record_type
  176.     | set_type
  177.     | file_type
  178.     ;
  179.  
  180. array_type : _ARRAY LBRAC index_list RBRAC _OF component_type
  181.     ;
  182.  
  183. index_list : index_list comma index_type
  184.     | index_type
  185.     ;
  186.  
  187. index_type : ordinal_type ;
  188.  
  189. ordinal_type : new_ordinal_type
  190.     | identifier
  191.     ;
  192.  
  193. component_type : type_denoter ;
  194.  
  195. record_type : _RECORD record_section_list _END
  196.     | _RECORD record_section_list semicolon variant_part _END
  197.     | _RECORD variant_part _END
  198.     ;
  199.  
  200. record_section_list : record_section_list semicolon record_section
  201.     | record_section
  202.     ;
  203.  
  204. record_section : identifier_list COLON type_denoter
  205.     ;
  206.  
  207. variant_part : _CASE variant_selector _OF variant_list semicolon
  208.     | _CASE variant_selector _OF variant_list
  209.     |
  210.     ;
  211.  
  212. variant_selector : tag_field COLON tag_type
  213.     | tag_type
  214.     ;
  215.  
  216. variant_list : variant_list semicolon variant
  217.     | variant
  218.     ;
  219.  
  220. variant : case_constant_list COLON LPAREN record_section_list RPAREN
  221.     | case_constant_list COLON LPAREN record_section_list semicolon
  222.         variant_part RPAREN
  223.     | case_constant_list COLON LPAREN variant_part RPAREN
  224.     ;
  225.  
  226. case_constant_list : case_constant_list comma case_constant
  227.     | case_constant
  228.     ;
  229.  
  230. case_constant : constant
  231.     | constant DOTDOT constant
  232.     ;
  233.  
  234. tag_field : identifier ;
  235.  
  236. tag_type : identifier ;
  237.  
  238. set_type : _SET _OF base_type
  239.     ;
  240.  
  241. base_type : ordinal_type ;
  242.  
  243. file_type : _FILE _OF component_type
  244.     ;
  245.  
  246. new_pointer_type : UPARROW domain_type
  247.     ;
  248.  
  249. domain_type : identifier ;
  250.  
  251. variable_declaration_part : _VAR variable_declaration_list semicolon
  252.     |
  253.     ;
  254.  
  255. variable_declaration_list :
  256.       variable_declaration_list semicolon variable_declaration
  257.     | variable_declaration
  258.     ;
  259.  
  260. variable_declaration : identifier_list COLON type_denoter
  261.     ;
  262.  
  263. procedure_and_function_declaration_part :
  264.         proc_or_func_declaration_list semicolon
  265.     |
  266.     ;
  267.  
  268. proc_or_func_declaration_list :
  269.       proc_or_func_declaration_list semicolon proc_or_func_declaration
  270.     | proc_or_func_declaration
  271.     ;
  272.  
  273. proc_or_func_declaration : procedure_declaration
  274.     | function_declaration
  275.     ;
  276.  
  277. procedure_declaration : procedure_heading semicolon directive
  278.     | procedure_heading semicolon procedure_block
  279.     ;
  280.  
  281. procedure_heading : procedure_identification
  282.     | procedure_identification formal_parameter_list
  283.     ;
  284.  
  285. directive : _FORWARD
  286.     | _EXTERNAL
  287.     ;
  288.  
  289. formal_parameter_list : LPAREN formal_parameter_section_list RPAREN ;
  290.  
  291. formal_parameter_section_list : formal_parameter_section_list semicolon
  292.  formal_parameter_section
  293.     | formal_parameter_section
  294.     ;
  295.  
  296. formal_parameter_section : value_parameter_specification
  297.     | variable_parameter_specification
  298.     | procedural_parameter_specification
  299.     | functional_parameter_specification
  300.     ;
  301.  
  302. value_parameter_specification : identifier_list COLON identifier
  303.     ;
  304.  
  305. variable_parameter_specification : _VAR identifier_list COLON identifier
  306.     ;
  307.  
  308. procedural_parameter_specification : procedure_heading ;
  309.  
  310. functional_parameter_specification : function_heading ;
  311.  
  312. procedure_identification : _PROCEDURE identifier ;
  313.  
  314. procedure_block : block ;
  315.  
  316. function_declaration : function_heading semicolon directive
  317.     | function_identification semicolon function_block
  318.     | function_heading semicolon function_block
  319.     ;
  320.  
  321. function_heading : _FUNCTION identifier COLON result_type
  322.     | _FUNCTION identifier formal_parameter_list COLON result_type
  323.     ;
  324.  
  325. result_type : identifier ;
  326.  
  327. function_identification : _FUNCTION identifier ;
  328.  
  329. function_block : block ;
  330.  
  331. statement_part : compound_statement ;
  332.  
  333. compound_statement : _BEGIN statement_sequence _END ;
  334.  
  335. statement_sequence : statement_sequence semicolon statement
  336.     | statement
  337.     | error
  338.  { writeln('statement ignored'); yyclearin }
  339.     ;
  340.  
  341. statement : open_statement
  342.     | closed_statement
  343.     ;
  344.  
  345. open_statement : label COLON non_labeled_open_statement
  346.     | non_labeled_open_statement
  347.     ;
  348.  
  349. closed_statement : label COLON non_labeled_closed_statement
  350.     | non_labeled_closed_statement
  351.     ;
  352.  
  353. non_labeled_closed_statement : assignment_statement
  354.     | procedure_statement
  355.     | goto_statement
  356.     | compound_statement
  357.     | case_statement
  358.     | repeat_statement
  359.     | closed_with_statement
  360.     | closed_if_statement
  361.     | closed_while_statement
  362.     | closed_for_statement
  363.     |
  364.     ;
  365.  
  366. non_labeled_open_statement : open_with_statement
  367.     | open_if_statement
  368.     | open_while_statement
  369.     | open_for_statement
  370.     ;
  371.  
  372. repeat_statement : _REPEAT statement_sequence _UNTIL boolean_expression
  373.     ;
  374.  
  375. open_while_statement : _WHILE boolean_expression _DO open_statement
  376.     ;
  377.  
  378. closed_while_statement : _WHILE boolean_expression _DO closed_statement
  379.     ;
  380.  
  381. open_for_statement : _FOR control_variable ASSIGNMENT initial_value direction
  382.             final_value _DO open_statement
  383.     ;
  384.  
  385. closed_for_statement : _FOR control_variable ASSIGNMENT initial_value direction
  386.             final_value _DO closed_statement
  387.     ;
  388.  
  389. open_with_statement : _WITH record_variable_list _DO open_statement
  390.     ;
  391.  
  392. closed_with_statement : _WITH record_variable_list _DO closed_statement
  393.     ;
  394.  
  395. open_if_statement : _IF boolean_expression _THEN statement
  396.     | _IF boolean_expression _THEN closed_statement _ELSE open_statement
  397.     ;
  398.  
  399. closed_if_statement : _IF boolean_expression _THEN closed_statement
  400.             _ELSE closed_statement
  401.     ;
  402.  
  403. assignment_statement : variable_access ASSIGNMENT expression
  404.     ;
  405.  
  406. variable_access : identifier
  407.     | indexed_variable
  408.     | field_designator
  409.     | variable_access UPARROW
  410.     ;
  411.  
  412. indexed_variable : variable_access LBRAC index_expression_list RBRAC
  413.     ;
  414.  
  415. index_expression_list : index_expression_list comma index_expression
  416.     | index_expression
  417.     ;
  418.  
  419. index_expression : expression ;
  420.  
  421. field_designator : variable_access DOT identifier
  422.     ;
  423.  
  424. procedure_statement : identifier params
  425.     | identifier
  426.     ;
  427.  
  428. params : LPAREN actual_parameter_list RPAREN ;
  429.  
  430. actual_parameter_list : actual_parameter_list comma actual_parameter
  431.     | actual_parameter
  432.     ;
  433.  
  434. /*
  435.  * this forces you to check all this to be sure that only write and
  436.  * writeln use the 2nd and 3rd forms, you really can't do it easily in
  437.  * the grammar, especially since write and writeln aren't reserved
  438.  */
  439. actual_parameter : expression
  440.     | expression COLON expression
  441.     | expression COLON expression COLON expression
  442.     ;
  443.  
  444. goto_statement : _GOTO label
  445.     ;
  446.  
  447. case_statement : _CASE case_index _OF case_list_element_list _END
  448.     | _CASE case_index _OF case_list_element_list SEMICOLON _END
  449.     | _CASE case_index _OF case_list_element_list semicolon
  450.             otherwisepart statement _END
  451.     | _CASE case_index _OF case_list_element_list semicolon
  452.             otherwisepart statement SEMICOLON _END
  453.     ;
  454.  
  455. case_index : expression ;
  456.  
  457. case_list_element_list : case_list_element_list semicolon case_list_element
  458.     | case_list_element
  459.     ;
  460.  
  461. case_list_element : case_constant_list COLON statement
  462.     ;
  463.  
  464. otherwisepart :    _OTHERWISE
  465.     | _OTHERWISE COLON
  466.     ;
  467.  
  468. control_variable : identifier ;
  469.  
  470. initial_value : expression ;
  471.  
  472. direction : _TO
  473.     | _DOWNTO
  474.     ;
  475.  
  476. final_value : expression ;
  477.  
  478. record_variable_list : record_variable_list comma variable_access
  479.     | variable_access
  480.     ;
  481.  
  482. boolean_expression : expression ;
  483.  
  484. expression : simple_expression
  485.     | simple_expression relop simple_expression
  486.     | error
  487.     ;
  488.  
  489. simple_expression : term
  490.     | simple_expression addop term
  491.     ;
  492.  
  493. term : factor
  494.     | term mulop factor
  495.     ;
  496.  
  497. factor : sign factor
  498.     | exponentiation
  499.     ;
  500.  
  501. exponentiation : primary
  502.     | primary STARSTAR exponentiation
  503.     ;
  504.  
  505. primary : variable_access
  506.     | unsigned_constant
  507.     | function_designator
  508.     | set_constructor
  509.     | LPAREN expression RPAREN
  510.     | _NOT primary
  511.     ;
  512.  
  513. unsigned_constant : unsigned_number
  514.     | CHARACTER_STRING
  515.     | _NIL
  516.     ;
  517.  
  518. unsigned_number : unsigned_integer | unsigned_real ;
  519.  
  520. unsigned_integer : DIGSEQ
  521.     ;
  522.  
  523. unsigned_real : REALNUMBER
  524.     ;
  525.  
  526. /* functions with no params will be handled by plain identifier */
  527. function_designator : identifier params
  528.     ;
  529.  
  530. set_constructor : LBRAC member_designator_list RBRAC
  531.     | LBRAC RBRAC
  532.     ;
  533.  
  534. member_designator_list : member_designator_list comma member_designator
  535.     | member_designator
  536.     ;
  537.  
  538. member_designator : member_designator DOTDOT expression
  539.     | expression
  540.     ;
  541.  
  542. addop: PLUS
  543.     | MINUS
  544.     | _OR
  545.     ;
  546.  
  547. mulop : STAR
  548.     | SLASH
  549.     | _DIV
  550.     | _MOD
  551.     | _AND
  552.     ;
  553.  
  554. relop : EQUAL
  555.     | NOTEQUAL
  556.     | LT
  557.     | GT
  558.     | LE
  559.     | GE
  560.     | _IN
  561.     ;
  562.  
  563. identifier : IDENTIFIER
  564.     ;
  565.  
  566. semicolon : SEMICOLON
  567.     ;
  568.  
  569. comma : COMMA
  570.     ;
  571.  
  572. %%
  573.  
  574. begin if yyparse=0 then end.